package com.atguigu.delivery.servlet;

import com.atguigu.delivery.entity.Delivery;
import com.atguigu.delivery.entity.SysUser;
import com.atguigu.delivery.service.api.DeliveryService;
import com.atguigu.delivery.service.impl.DeliveryServiceImpl;
import com.atguigu.delivery.util.DeliveryConstant;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;
import java.util.List;

@WebServlet(urlPatterns = "/DeliveryServlet/List")
public class DeliveryServletList extends HttpServlet {

    private DeliveryService deliveryService = new DeliveryServiceImpl();

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 1、获取 HttpSession 对象
        HttpSession session = request.getSession();

        // 2、从会话域获取 SysUser 对象
        SysUser sysUser = (SysUser) session.getAttribute(DeliveryConstant.ATTR_NAME_LOGIN_USER);

        // 3、获取 userId 值
        Integer userId = sysUser.getId();

        // 4、调用 Service 方法查询数据
        List<Delivery> deliveryList = deliveryService.getDeliveryListByUserId(userId);

        // 5、将查询结果保存到请求域中
        request.setAttribute("deliveryList", deliveryList);

        // 6、转发请求
        request.getRequestDispatcher("/WEB-INF/pages/list.jsp").forward(request, response);
    }
}
